﻿using BaoVeThanhDia.GamePlay.Monsters;
using BaoVeThanhDia.MapFolder;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;

namespace BaoVeThanhDia.GamePlay.Towers
{
    class MagicTower : Tower
    {
        private float countDraw;// biến phụ đếm thời gian
        private Texture2D imageBall;
        public MagicTower() { }
        public MagicTower(Vector2 index, int level, int type)
        {
            sound = new Sound.CustomSoundEffect("sounds/effect/Tower/Phep_Thuat");
            ValueBuff = 0;
            countDraw = 0;
            setIndex(index);
            setLevel(level, type);
            weapons = new List<Weapon>();
            setelement();
        }
        public override void Load(ContentManager content)
        {
            sound.Load(content);
            scale = 128 / 100f;
            Image = content.Load<Texture2D>("images/towers/magic/" + Level.ToString() + (Type == 0 ? "" : Type.ToString()));
            imageBall = content.Load<Texture2D>("images/towers/magic/ball" + (Type == 2 ? "2" : "1"));
        }
        public override void setLevel(int level, int type)
        {
            Level = level;
            Type = type;
            if (Type == 1) Effect = "Slow";
            else if (Type == 2) Effect = "Poison";
            else Effect = "None";
        }
        public override void Draw(ContentManager cont, SpriteBatch sprite)
        {
            if (ValueBuff > 0)
            {
                ImageBuffed = cont.Load<Texture2D>("images/towers/soul/root" + (ValueBuff < 30 ? "" : (ValueBuff < 50 ? "1" : "2")));
                sprite.Draw(ImageBuffed, (Position + new Vector2(-13, -45)) * GamePage.Scale + GamePage.Dolech, new Rectangle(((int)countDraw / 5) % 6 * 100, 0, 100, 100), Color.White, 0f, Vector2.Zero, 1.5f * GamePage.Scale, SpriteEffects.None, 0f);
            }
            sprite.Draw(Image, (Position + new Vector2(0, -60)) * GamePage.Scale + GamePage.Dolech, new Rectangle(((int)(countDraw / 6) % 5) * 100, 0, 100, 133), Color.White, 0f, Vector2.Zero, scale * GamePage.Scale, SpriteEffects.None, 0f);
            if (weapons.Count == 0)
            {
                sprite.Draw(imageBall, (Position + new Vector2(10, -120)) * GamePage.Scale + GamePage.Dolech, new Rectangle(((int)(countDraw / 6) % 4) * 69, 0, 69, 69), Color.White, 0f, Vector2.Zero, 1.4f * GamePage.Scale, SpriteEffects.None, 0f);
            }
            else
            {
                DrawWeapon(cont, sprite);
            }
            countDraw += Speed * GamePage.Speed / 2;
            if (countDraw >= 120) countDraw = 0;
        }
        public override void OptionDraw(SpriteBatch sprite, Vector2 posi, Vector2 dolech, float scale, bool isTouch)
        {
            sprite.Draw(Image, posi + dolech, new Rectangle(((int)(countDraw / 6) % 5) * 100, 0, 100, 133), Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
            sprite.Draw(imageBall, posi + new Vector2(15, -35) * scale + dolech, new Rectangle(((int)(countDraw / 6) % 4) * 69, 0, 69, 69), Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
            if (isTouch)
            {
                countDraw += Speed;
                if (countDraw >= 120) countDraw = 0;
            }
        }
        public override void Ban()
        {
            if (((int)countDraw / 6) % 5 == 4 && (int)countDraw % 6 == 0)
            {
                int temp = 0;
                for (int i = 0; i < GamePage.monsters.Count; i++)
                {
                    if (Vector2.Distance(Position, GamePage.monsters[i].getPosition()) <= Range * 128 && GamePage.monsters[i].getCurHP() > 0)
                    {
                        sound.Play(0.7f);
                        if (Type == 0)
                        {
                            weapons.Add(new Weapon(4, PhysicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), MagicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), 25, Position + new Vector2(50, -120) * GamePage.Scale, GamePage.monsters[i].getPosition() + new Vector2(64, 64) * GamePage.Scale, i, Effect, 0, countEffect));
                            break;
                        }
                        else if (Type == 1)
                        {
                            if (temp < Level - 1)
                            {
                                temp++;
                                weapons.Add(new Weapon(4, PhysicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), MagicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), 25, Position + new Vector2(50, -120) * GamePage.Scale, GamePage.monsters[i].getPosition() + new Vector2(64, 64) * GamePage.Scale, i, Effect, 60, countEffect));
                            }
                            else break;
                        }
                        else
                        {
                            weapons.Add(new Weapon(5, PhysicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), MagicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), 25, Position + new Vector2(50, -120) * GamePage.Scale, new Vector2(Position.X + 50 * GamePage.Scale, 0)/*lên trên*/, -1, Effect, 30, countEffect));
                            weapons.Add(new Weapon(5, PhysicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), MagicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), 25, Position + new Vector2(50, -120) * GamePage.Scale, new Vector2(Position.X + 50 * GamePage.Scale, Map.SizeOfMatrix.X * 128)/*xuống dưới*/, -1, Effect, 30, countEffect));
                            weapons.Add(new Weapon(5, PhysicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), MagicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), 25, Position + new Vector2(50, -120) * GamePage.Scale, new Vector2(0, Position.Y - 120 * GamePage.Scale)/*sang trái*/, -1, Effect, 30, countEffect));
                            weapons.Add(new Weapon(5, PhysicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), MagicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), 25, Position + new Vector2(50, -120) * GamePage.Scale, new Vector2(Map.SizeOfMatrix.Y * 128, Position.Y - 120 * GamePage.Scale)/*sang phải*/, -1, Effect, 30, countEffect));
                            break;
                        }
                    }
                }
            }
        }
        public override void setelement()
        {
            if (Level == 1)
            {
                Price = 100;
                PriceBought = 100;
                PhysicDame = 0;
                MagicDame = 30;
                Speed = 0.8f;
                Range = 1.75f;
                countEffect = 0;
            }
            else if (Level == 2)
            {
                Price = 50;
                PriceBought = 150;
                PhysicDame = 0;
                MagicDame = 40;
                Speed = 0.9f;
                Range = 2;
                countEffect = 0;
            }
            else if (Level == 3)
            {

                if (Type == 1)
                {
                    Price = 75;
                    PriceBought = 225;
                    PhysicDame = 0;
                    MagicDame = 55;
                    Speed = 1;
                    countEffect = 25;
                    Range = 2.25f;
                }
                else
                {
                    Price = 100;
                    PriceBought = 250;
                    PhysicDame = 0;
                    MagicDame = 20;
                    Speed = 0.6f;
                    countEffect = 2;
                    Range = 2f;
                }
            }
            else
            {
                if (Type == 1)
                {
                    Range = 2.5f;
                    Price = 75;
                    PriceBought = 300;
                    PhysicDame = 0;
                    MagicDame = 60;
                    Speed = 1f;
                    countEffect = 50;
                }
                else
                {
                    Range = 2f;
                    Price = 125;
                    PriceBought = 375;
                    PhysicDame = 0;
                    MagicDame = 25;
                    Speed = 0.7f;
                    countEffect = 3;
                }
            }
            getUpGradeValue();
        } // dành riêng cho tháp ma thuật
        private void getUpGradeValue()
        {
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[20])
            {
                Price -= (Price * 3) / 20;
                PriceBought -= (PriceBought * 3) / 20;
            }
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[21])
            {
                Range += (Range * 3) / 20;
            }
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[22])
            {
                MagicDame += (MagicDame * 3) / 20;
                if (Type == 2)
                    countEffect += (countEffect * 3) / 20;
            }
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[23])
            {
                // chưa biết
            }
        }
        public override float getPriceUpGrade(int level, int type)
        {
            return new MagicTower(Vector2.Zero, level, type).Price;
        }
        public override void DrawPictureUpGrade(int level, int type, ContentManager cont, SpriteBatch sprite, Vector2 posi)
        {
            MagicTower temp = new MagicTower(Vector2.Zero, level, type);
            temp.Load(cont);
            temp.OptionDraw(sprite, posi + new Vector2(8, -10) * GamePage.Scale, GamePage.Dolech, 0.72f * GamePage.Scale, false);
        }
    }
}
